package com.sl.springboot.service.impl;

import com.sl.springboot.dao.CourseDao;
import com.sl.springboot.model.Course;
import com.sl.springboot.model.PageInfo;
import com.sl.springboot.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import com.sl.springboot.util.RandomIdUtil;

/**
 * 用户Service接口实现类
 * @author: mty
 */
@Service("courseService")
@Transactional
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseDao courseDao;


    //分页查询
    @Override
    public PageInfo<Course> findPageInfo(Integer pageIndex, Integer pageSize,Map mp) {
        PageInfo<Course> pi = new PageInfo<Course>();
        pi.setPageIndex(pageIndex);
        pi.setPageSize(pageSize);
        mp.put("currentPage",(pi.getPageIndex()-1)*pi.getPageSize());
        mp.put("pageSize",pi.getPageSize());
        //获取总条数
        Integer totalCount = courseDao.totalCount(mp);
        if (totalCount>0){
            pi.setTotalCount(totalCount);
            //按条件查询
            List<Course> courseList =	courseDao.getCourseList(mp);
            pi.setList(courseList);
        }
        return pi;
    }


    //添加
    @Override
    public int addCourse(Course course) {
        course.setId(RandomIdUtil.getRandomIdByUUID());
        return courseDao.addCourse(course);
    }


    //根据id删除
    @Override
    public int deleteCourse(String id) {
        return courseDao.deleteCourse(id);
    }


    //修改宿舍信息
    @Override
    public int updateCourse(Course course) {
        return courseDao.updateCourse(course);
    }

    //根据ID查询
    @Override
    public Course findCourseById (String id){
        Course d = courseDao.findCourseById(id);
        return  d;
    }

    //查询所有
    @Override
    public List<Course> getAll(){
        List<Course> courseList = courseDao.getAll();
        return courseList;
    }

    //按照条件查询
    @Override
    public List<Course> queryFilter(Map mp){
        List<Course> courseList = courseDao.queryFilter(mp);
        return courseList;
    }
}
